/*
 * Copyright (C) 2012  Romain DUBOIS
 * 
 * This file is part of NeMoS - Network and Modular Software
 * 
 * NeMoS is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * 
 * NeMoS is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with NeMoS.  If not, see <http://www.gnu.org/licenses/>.
 */
package nemos.remote.event;

import nemos.remote.IInterceptor;
import nemos.remote.RemoteCall;

import org.apache.felix.scr.annotations.Component;
import org.apache.felix.scr.annotations.Property;
import org.apache.felix.scr.annotations.Service;
import org.osgi.service.event.Event;

/**
 * RemoteEventInterceptor : The remote events interceptor class.
 * 
 * <p>
 * This class intercepts events sended to the {@link RemoteEventHandler} and set
 * the source framework property.
 * </p>
 * 
 * @author Romain DUBOIS
 */
@Service(IInterceptor.class)
@Property(name = IInterceptor.INTERCEPTOR_SCOPE, value = RemoteEventInterceptor.TARGET_FILTER)
@Component
public class RemoteEventInterceptor implements IInterceptor {

    /** The target interceptor filter */
    static final String TARGET_FILTER = "(service.pid=" + RemoteEventHandler.SERVICE_PID + ")"; //$NON-NLS-1$ //$NON-NLS-2$

    /**
     * Constructor
     */
    public RemoteEventInterceptor() {
        super();
    }

    /**
     * {@inheritDoc}
     * 
     * @see nemos.remote.IInterceptor#interceptInvocation(nemos.remote.RemoteCall)
     */
    @Override
    public void interceptInvocation(final RemoteCall pCall) {
        final Event lOrgEvent = (Event) pCall.getParameters()[0];

        // Replace event
        pCall.getParameters()[0] = RemoteEventHandler.addFramework(lOrgEvent,
                pCall.getFrameworkUuid());
    }
}
